<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
class Tg extends Base
{
    /**
     * 异地登录验证
     * @return string|void
     */
    public function _initialize()
    {
        extract(input());
        if($user_id != 0){
            $auth = $this->checkAll($user_id,$time,$value,$reqKey);
            if($auth == 112){
                echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
                exit;
            }elseif($auth == 110){
                echo json_encode(['status' => 302, 'msg' => $this->frost]);
                exit;
            }elseif($auth == 1103){
                echo json_encode(['status' => 303, 'msg' => $this->del]);
                exit;
            }
        }

    }
    public function index()
    {
        extract(input());
        $can = $this->getSonByUser($user_id);
        if($can['status'] != 200){
            return json($can);
        }
        $sondata = $can['msg'];
        $today = date('Y-m-d');
        $data = Db::table('up_offline_tg')
            ->where('school_id',$sondata['uson_school'])
            ->where('year',$sondata['uson_year'])
            ->where('status',1)
            ->where('yes',2)
            ->where('startdate','<=',$today)
            ->where('enddate','>=',$today)
            ->find();
        if(!$data){
            return json(['status' => 100, 'msg' => '当前时间此学校当前届级没有统购订单']);
        }else{
            $havebuy = Db::table('up_offline_tgorder')
                ->where('user_id',$user_id)
                ->where('status',2)
                ->where('fromtype','>',1)
                ->where('tg_id',$data['id'])
                ->find();
            $have2 = Db::table('up_offline_tgorder')
                ->where('user_id',$user_id)
                ->where('fromtype',1)
                ->where('tg_id',$data['id'])
                ->find();
            if($havebuy || $have2){
                return json(['status' => 100, 'msg' => '您已经支付或者采集过了']);
            }else{
                return json(['status' => 200, 'msg' => $data]);
            }
        }

    }
    public function buyTg()
    {
        $user_id = $_POST['user_id'];
        $can = $this->getSonByUser($user_id);
        if($can['status'] != 200){
            return json($can);
        }
        $sondata = $can['msg'];
        $uson_id = $sondata['uson_id'];
        $id = $_POST['id'];
        $data = Db::table('up_offline_tg')
            ->where('id', $id)
            ->find();
        if(empty($data)){
            return json(['status' => 100, 'msg' => '不存在此学校统购信息，请核实']);
        }
        if($data['status'] != 1){
            return json(['status' => 100, 'msg' => '此学校统购已过期']);
        }
        $school = $data['school'];
        $grade = $_POST['grade'];
        $class = $_POST['class'];
        $name = $_POST['name'];
        $sex = $_POST['sex'] == 1 ? '男' : '女';
        $idcard = $data['year'];
        $money = 0;
        $type = 1;
        $send_name = $_POST['send_name'];
        $send_address = $_POST['send_address'];
        $order = [
            'orderno' => 'tg' . time() . rand(100000,999999),
            'tg_id' => $id,
            'user_id' => $user_id,
            'uson_id' => $uson_id,
            'school' => $school,
            'grade' => $grade,
            'class' => $class,
            'name' => $name,
            'sex' => $sex,
            'idcard' => $idcard,
            'money' => $money,
            'time' => date('Y-m-d H:i:s'),
            'type' => $type,
            'sale_can' => 2,
            'send_name' => $send_name,
            'send_address' => $send_address
        ];
        //-------------------------------------------------------------------------
        if($data['usetype'] != 2){
            $order['height'] = $_POST['height'];
            $order['weight'] = $_POST['weight'];
            $order['size'] = $_POST['size'];
            $order['sw_j'] = $_POST['sw_j'];
            $order['sw_x'] = $_POST['sw_x'];
            $order['sw_y'] = $_POST['sw_y'];
            $order['sw_t'] = $_POST['sw_t'];
            $order['sw_b'] = $_POST['sw_b'];
            $order['sw_k'] = $_POST['sw_k'];
        }
        if($data['usetype'] != 1){
            if($data['money'] <= 0){
                $order['money'] = $_POST['money'];
            }else{
                $order['money'] = $data['money'];
            }
            $order['type'] = $_POST['type'];
            if($order['money'] <= 0){
                return json(['status' => 100, 'msg' => '金额不能低于0']);
            }
            $order['sale_can'] = $data['shouhou'];
        }
        $order['phone'] = $_POST['phone'];
        $order['fromtype'] = $data['usetype'];
        if(!preg_match("/^1[123456789]\d{9}$/", $_POST['phone'])){
            return json(['status'=>1,'msg'=>'请填写正确的手机号码']);
        }
        //-------------------------------------------------------------------------
        $res = Db::table('up_offline_tgorder')->insert($order);
        if($res){
            if($data['usetype'] == 1){
                return json(['status' => 200, 'msg' => '提交成功']);
            }else{
                $pay_type = $order['type'];
                if($pay_type == 1){
                    $payorder = array();
                    $payorder['body'] = "$school-$name"; // 描述
                    $payorder['total_fee'] = $order['money']*100; // 订单金额
                    $payorder['out_trade_no'] = $order['orderno']; // 订单号
                    $payorder['product_id'] = ''; // 产品ID
                    $payorder['trade_type'] = 'APP'; // 类型
                    $mww_weixinpay =  new \app\api\controller\Weixin();
                    $respont = $mww_weixinpay->WeChatPayReady($payorder);
                    return json(['status'=>200,'msg'=>$respont]);
                }elseif($pay_type == 2){
                    $mww_alipay =  new \app\api\controller\Ali();
                    $respont = $mww_alipay->getParam($order['orderno'], $order['money'], '统购APP支付', "$school-$name");
                    return json($respont);
                }else{
                    return json(['status'=>0,'msg'=>'暂未开放，敬请期待~']);
                    $mww_yinlianpay =  new \app\api\controller\Upacp();
                    $respont = $mww_yinlianpay->UpacpPayTn($order['orderno'], $order['money']*100);
                    //生产环境APP端请求地址：https://gateway.95516.com/gateway/api/appTransReq.do
                    $respont['app_url'] = 'https://gateway.test.95516.com/gateway/api/appTransReq.do';
                    return json($respont);
                }
            }
        }else{
            return json(['status' => 100, 'msg' => '加入订单失败，请刷新重试！']);
        }
    }
    /**
     * 我的统购订单
     */
    public function myTg()
    {
        $user_id = $_POST['user_id'];
        $data = Db::table('up_offline_tgorder')
            ->where('user_id',$user_id)
            ->where('status',2)
            ->where(function ($query) use ($user_id) {
                $query->where('user_id', $user_id)->where('status', 2)->where('fromtype','>',1);
            })
            ->whereOr(function ($query) use ($user_id) {
                $query->where('user_id', $user_id)->where('fromtype', 1);
            })
            ->order('time desc')
            ->select();
        return json(['status' => 200, 'msg' => $data]);
    }
    /**
     * 我的统购订单详情
     */
    public function myTgInfo()
    {
        $id = $_POST['id'];
        $data = Db::table('up_offline_tgorder')
            ->where('id',$id)
            ->find();
        return json(['status' => 200, 'msg' => $data]);
    }
    /**
     * 申请售后
     */
    public function myTgSaleAsk()
    {
        $id = $_POST['id'];
        $sale_reason = $_POST['sale_reason'];
        $data = Db::table('up_offline_tgorder')
            ->where('id',$id)
            ->find();
        if($data['status'] == 2 && $data['sale_can'] == 1 && $data['sale_status'] == 1 && $data['fromtype'] > 1){
            $res = Db::table('up_offline_tgorder')
                ->where('id',$id)->update([
                    'tui_no' => 'tgt' . time() . rand(10000,99999),
                    'sale_status' => 2,
                    'sale_time' => date('Y-m-d H:i:s'),
                    'sale_reason' => $sale_reason
                ]);
            if($res){
                return json(['status' => 200, 'msg' => '售后已提交，请等待审核~']);
            }else{
                return json(['status' => 100, 'msg' => '申请失败，请刷新重试']);
            }
        }else{
            return json(['status' => 100, 'msg' => '只有未售后过的付款订单才允许售后']);
        }

    }
}
